﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;

namespace iLangl.LanguageWire.DAL.iLanglDAL.XMLStorage
{
	public class ProjectDAL : BaseStorageDAL, IProjectDAL
	{
		public void RegisterProject(Project project, IDbTransaction transaction)
		{
			dtoXMLStorage.ProjectRow projectRow = Storage.Project.FindById(project.Id);
			bool isNew = (projectRow == null);
			if (isNew)
			{
				projectRow = Storage.Project.NewProjectRow();
			}

			projectRow.Deadline = project.Deadline;
			projectRow.Description = project.Description;
			projectRow.Id = project.Id;
			projectRow.Name = project.Name;
			

			if (!String.IsNullOrEmpty(project.TPIdentity))
				projectRow.TPIdentity = project.TPIdentity;

			if (project.Owner != null)
				projectRow.OwnerId = project.Owner.Id;

			if (project.Product != null)
				projectRow.ProductId = project.Product.Id;

			projectRow.Status = project.Status;
			if (project.TechnicalContact != null)
				projectRow.TechnicalContactId = project.TechnicalContact.Id;

			if (project.WorkArea != null)
				projectRow.WorkAreaId = project.WorkArea.Id;

			if (isNew)
			{
				Storage.Project.AddProjectRow(projectRow);
				projectRow.TimeSpan = CurrentTimeSpan;
				UpdateDeliveryTime(eEntityType.Project, project.Id, projectRow.TimeSpan);
			}

			foreach (ContentEntity ce in project.Contents)
			{
				CreateContent(ce, transaction);
			}

			foreach (Translation translation in project.Translations)
			{
				DALFacade.Instance.TranslationDAL.CreateTranslation(translation, transaction);
			}

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public void CreateContent(ContentEntity content, IDbTransaction transaction)
		{
			dtoXMLStorage.ContentRow contentRow = Storage.Content.FindById(content.Id);

			bool isNew = (contentRow == null);

			if (isNew)
			{
				contentRow = Storage.Content.NewContentRow();
			}

			contentRow.Content = content.Content;
			contentRow.Id = content.Id;
			if (content.Language != null)
				contentRow.LanguageCode = content.Language.LanguageId;

			contentRow.Name = content.Name;
			if (content.Project != null)
				contentRow.ProjectId = content.Project.Id;

			contentRow.Type = content.Type;
			contentRow.Version = content.Version;
			if (!String.IsNullOrEmpty(content.TPIdentity))
				contentRow.TPIdentity = content.TPIdentity;

			if (isNew)
			{
				Storage.Content.AddContentRow(contentRow);
				contentRow.TimeSpan = CurrentTimeSpan;
				UpdateDeliveryTime(eEntityType.Content, content.Id, contentRow.TimeSpan);
			}


			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		

		
		

		#region Update Methods


		public void UpdateContentReference(string ilanglContentId, Int32 LWContentId, IDbTransaction transaction)
		{
			dtoXMLStorage.ContentRow cr = Storage.Content.FindById(ilanglContentId);

			if (cr == null)
				return;
			
			cr.TPIdentity = LWContentId.ToString();
			cr.TimeSpan = CurrentTimeSpan;

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public void UpdateProjectReference(string ilanglProjectId, int LWProjectId, IDbTransaction transaction)
		{
			dtoXMLStorage.ProjectRow pr = Storage.Project.FindById(ilanglProjectId);

			if (pr == null)
				return;

			pr.TPIdentity = LWProjectId.ToString();
			pr.TimeSpan = CurrentTimeSpan;

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public void UpdateProjectStatus(Project project, IDbTransaction transaction)
		{
			dtoXMLStorage.ProjectRow pr = Storage.Project.FindById(project.Id);

			if (pr == null)
				return;

			pr.Status = project.Status;
			pr.TimeSpan = CurrentTimeSpan;

			if (transaction == null)
			{
				AcceptChanges();
			}
		}


		
		#endregion

		#region Read Section
		

		public ContentEntity ReadContent(string contentId)
		{
			dtoXMLStorage.ContentRow contentrow = Storage.Content.FindById(contentId);


			ContentEntity content = new ContentEntity();
			content.Id = contentrow.Id;
			content.Language = new Language();
			content.Language.LanguageId = contentrow.LanguageCode;
			content.Content = contentrow.Content;
			content.Name = contentrow.Name;
			content.Type = contentrow.Type;
			content.Version = contentrow.Version;

			if (!contentrow.IsProjectIdNull())
			{
				content.Project = new Project();
				content.Project.Id = contentrow.ProjectId;
			}

			if (!contentrow.IsTPIdentityNull())
				content.TPIdentity = contentrow.TPIdentity;

			return content;
		}

		public Project ReadProject(string projectId)
		{
			Project result = new Project();
			dtoXMLStorage.ProjectRow projectRow = Storage.Project.FindById(projectId);

			if (projectRow == null)
			{
				throw new ApplicationException(String.Format("Project with Id='{0}' not found!", projectId));
			}

			result.Id = projectRow.Id;
			result.Deadline = projectRow.Deadline;
			result.Description = projectRow.Description;
			result.Name = projectRow.Name;

			
			if (!projectRow.IsTPIdentityNull())
				result.TPIdentity = projectRow.TPIdentity;

			result.WorkArea = new WorkArea();
			result.WorkArea.Id = projectRow.WorkAreaId;
			result.Owner = new Contact();
			result.Owner.Id = projectRow.OwnerId;

			result.TechnicalContact = new Contact();
			result.TechnicalContact.Id = projectRow.TechnicalContactId;

			result.Product = new Product();
			result.Product.Id = projectRow.ProductId;


			return result;

		}
		#endregion


		#region IProjectDAL Members

		public void UpdateProject(Project project, IDbTransaction transaction)
		{
			RegisterProject(project, transaction);
		}

		#endregion
	}
}
